package com.chat.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.chat.bean.WsMessageDTO;
import com.chat.websocket.WebsocketServer;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * websocket关闭连接通知推送消费者
 *
 * @author mhw
 * @version v1.0
 * @date 2019-11-19
 */
@Slf4j
@Component
@RocketMQMessageListener(
        topic = "wsForcedTopic",
        consumerGroup = "wsForcedGroup",
        selectorExpression = "wsForcedTag"
)
public class WsForcedCloseConsumer implements RocketMQListener<String> {

    private final WebsocketServer websocketServer;

    @Autowired
    public WsForcedCloseConsumer(WebsocketServer websocketServer) {
        this.websocketServer = websocketServer;
    }

    @Override
    public void onMessage(String json) {
        log.info("ws强制关闭，消费端测试，数据：【{}】", json);
        WsMessageDTO wsMessageDTO = JSON.parseObject(json, WsMessageDTO.class);
        websocketServer.beForcedClose(wsMessageDTO.getUid());
    }
}
